#include "dialog.h"
#include <QGridLayout>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    createCtrlFrame();
    creatContentFrame();

    QHBoxLayout *mainLayout =new QHBoxLayout(this);
    mainLayout->addWidget(m_ctrlFrame);
    mainLayout->addWidget(m_contentFrame);
}

Dialog::~Dialog()
{

}

void Dialog::createCtrlFrame()
{
    m_ctrlFrame=new QFrame;
    m_ctrlFrame->setFrameStyle(QFrame::Sunken | QFrame::Box);     //Sunken 凹陷的

    m_windowLabel =new QLabel("QPalette::Window:");
    m_windowComboBox =new QComboBox;
    fillColorList(m_windowComboBox);
    connect(m_windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow(int)));   //当选中组合框中某个选项时,activate触发

    m_windowTextLabel =new QLabel("QPalette:WindowText:");
    m_windowTextComboBox =new QComboBox;
    fillColorList(m_windowTextComboBox);
    connect(m_windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText(int)));

    m_buttonLabel=new QLabel("QPatte:Button:");
    m_buttonComboBox =new QComboBox;
    fillColorList(m_buttonComboBox);
    connect(m_buttonComboBox,SIGNAL(activated(int)),this,SLOT(ShowButton(int)));

     m_buttonTextLabel =new QLabel("QPatte::ButtonText:");
     m_buttomTextComboBox =new QComboBox;
     fillColorList(m_buttomTextComboBox);
     connect(m_buttomTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowButtonText(int)));

     m_baseLabel =new QLabel("QPalette::Base:");
     m_baseComboBox =new QComboBox;
     fillColorList(m_baseComboBox);
     connect(m_baseComboBox,SIGNAL(activated(int)),this,SLOT(ShowBase(int)));

     QGridLayout *mainLayout =new QGridLayout(m_ctrlFrame);
    mainLayout->setSpacing(20);       //设置布局中子控件的间隙
    mainLayout->addWidget(m_windowLabel,0,0);
    mainLayout->addWidget(m_windowComboBox,0,1);
    mainLayout->addWidget(m_windowTextLabel,1,0);
    mainLayout->addWidget(m_windowTextComboBox,1,1);
    mainLayout->addWidget(m_buttonLabel,2,0);
    mainLayout->addWidget(m_buttonComboBox,2,1);
    mainLayout->addWidget(m_buttonTextLabel,3,0);
    mainLayout->addWidget(m_buttomTextComboBox,3,1);
    mainLayout->addWidget(m_baseLabel,4,0);
    mainLayout->addWidget(m_baseComboBox,4,1);

}

void Dialog::creatContentFrame()
{
    m_contentFrame =new QFrame;
    m_contentFrame->setAutoFillBackground(true);    //设置背景填充属性

    m_label1 = new QLabel("请选择一个值");
    m_label2 = new  QLabel("请输入字符串");
    m_comboBox1 =new QComboBox;
    m_lineEdit = new QLineEdit;
    m_textEdit =new QTextEdit;
    m_okBtn =new QPushButton(QString("确认"));
    m_cancelBtn =new QPushButton(QString("取消"));
    m_okBtn->setAutoFillBackground(true);
    m_cancelBtn->setAutoFillBackground(true);


    QGridLayout *Toplayout = new QGridLayout;
    Toplayout->addWidget(m_label1,0,0);
    Toplayout->addWidget(m_comboBox1,0,1);
    Toplayout->addWidget(m_label2,1,0);
    Toplayout->addWidget(m_lineEdit,1,1);
    Toplayout->addWidget(m_textEdit,2,0,1,2);     //占一行两列

    QHBoxLayout *bottomLayout =new  QHBoxLayout;
    bottomLayout->addWidget(m_okBtn);
    bottomLayout->addWidget(m_cancelBtn);

    QVBoxLayout *mainLayout =new QVBoxLayout(m_contentFrame);
    mainLayout->addLayout(Toplayout);
    mainLayout->addLayout(bottomLayout);
}

void Dialog::fillColorList(QComboBox *comboBox)
{
    QStringList colorList =QColor::colorNames();
    QString color;
    foreach (color, colorList)
    {
        //挨个读取颜色，将颜色填充到pix里
        QPixmap pix(QSize(70,20));
        pix.fill(QColor(color));
        comboBox->addItem(QIcon(pix),NULL);
        comboBox->setIconSize(QSize(70,20));

        comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);  //设置组合框的尺寸策略
    }
}

void Dialog::ShowWindow(int index)
{
    QStringList colorList =QColor::colorNames();
    QColor color =QColor(colorList[index]);
    QPalette p = m_contentFrame->palette();       //获取调色板
    p.setColor(QPalette::Window,color);
    m_contentFrame->setPalette(p);
    m_contentFrame->update();                   //刷新，重绘
}


void Dialog::ShowWindowText(int index)
{
    QStringList colorList =QColor::colorNames();
    QColor color = QColor(colorList[index]);
    QPalette p =m_contentFrame->palette();
    p.setColor(QPalette::WindowText,color);
    m_contentFrame->setPalette(p);
    m_contentFrame->update();
}

void Dialog::ShowButton(int index)
{
    QStringList colorList =QColor::colorNames();
    QColor color =QColor(colorList[index]);
    QPalette p = m_contentFrame->palette();
    p.setColor(QPalette::Button,color);
    m_contentFrame->setPalette(p);
    m_contentFrame->update();
}

void Dialog::ShowButtonText(int index)
{
    QStringList colorList =QColor::colorNames();
    QColor color =QColor(colorList[index]);
    QPalette p =m_contentFrame->palette();
    p.setColor(QPalette::ButtonText,color);
    m_contentFrame->setPalette(p);
    m_contentFrame->update();
}

void Dialog::ShowBase(int index)
{
    QStringList colorList =QColor::colorNames();
    QColor color =QColor(colorList[index]);
    QPalette p =m_contentFrame->palette();
    p.setColor(QPalette::Base,color);
    m_contentFrame->setPalette(p);
    m_contentFrame->update();

}
